(function () {
    var browser = UM.browser;
    UM.plugins['accessory'] = function () {
        var me = this;

        me.commands['accessory'] = {
            execCommand: function (cmd) {
                $("#file").click();
                $('#file').unbind().change(function (e) {
                    window.top.swal({
                        title: '上传中！请耐心等待...',
                        showConfirmButton: false,
                        type: 'success',
                        allowOutsideClick: false
                    });
                    var uploadUrl = $(this).parent().attr('uploadeFileUrl');
                    var formData = new FormData();
                    var file = $(this)[0].files[0];
                    formData.append('file' , file);
                    $.ajax({
                        url:uploadUrl,
                        method:'post',
                        dataType:'json',
                        data:formData,
                        processData:false,
                        contentType:false,
                        success:function (res) {
                            window.top.swal.close();
                            console.log(res.url);
                            me.execCommand( "inserthtml",'<a href="'+res.url+'" target="_blank" >'+res.originalName+'</a>' );
                        },
                        error:function (res) {
                            window.top.swal.close();
                            window.top.swal.showError('错误', res.error);
                        }
                    })

                })
            }
        }

    };
    UM.registerUI('accessory',
        function(name) {
            var me = this;
            var $btn = $.eduibutton({
                icon : name,
                click : function(){
                    me.execCommand(name);
                },
                cssRules :'border:1px solid red;',
                title: '附件上传'
            });
            this.addListener('selectionchange',function(){
                var state = this.queryCommandState(name);
                $btn.edui().disabled(state == -1).active(state == 1)
            });
            return $btn;
        }
    );

})();